package com.gopay.common.rcs.service.antiphish.manager.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gopay.common.constants.rcs.AntiPhishCheckOrderIpEnum;
import com.gopay.common.constants.rcs.AntiPhishEditNatureEnum;
import com.gopay.common.constants.rcs.AntiPhishGetwayChannelEnum;
import com.gopay.common.constants.trans.GatewayChannel;
import com.gopay.common.domain.rcs.RcsAntiPhishConfEditLog;
import com.gopay.common.domain.rcs.RcsAntiPhishConfGlobal;
import com.gopay.common.rcs.bean.IllegalReqDicision;
import com.gopay.common.rcs.manager.antiphish.RcsAntiPhishingGlobalManager;
import com.gopay.common.rcs.service.antiphish.manager.RcsAntiPhishingManagerGlobalService;
import com.gopay.common.rcs.vo.RcsAntiPhishConfGlobalVo;

/**
 * 全局防钓鱼配置：修改、查询
 * 
 * @RcsAntiPhishingManagerGlobalService.java
 * @author liujy
 * @2013-7-22 下午2:40:07 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("rcsAntiPhishingManagerGlobalService")
public class RcsAntiPhishingManagerGlobalServiceImpl implements RcsAntiPhishingManagerGlobalService {

    @Resource(name = "rcsAntiPhishingGlobalManager")
    RcsAntiPhishingGlobalManager rcsAntiPhishingGlobalManager;

    /*
     * 保存钓鱼规则信息
     * 
     * @see com.gopay.common.rcs.service.antiphish.manager.
     * RcsAntiPhishingManagerGlobalService#save(java.util.List)
     */
    @Override
    public void save(List<RcsAntiPhishConfGlobal> globalConfList) {
        rcsAntiPhishingGlobalManager.saveGlobalConf(globalConfList);
    }

    /*
     * 查询钓鱼规则
     * 
     * @see com.gopay.common.rcs.service.antiphish.manager.
     * RcsAntiPhishingManagerGlobalService#getGlobalConfBySql()
     */
    @Override
    public RcsAntiPhishConfGlobalVo getGlobalConfBySql() {

        RcsAntiPhishConfGlobalVo confVo = new RcsAntiPhishConfGlobalVo();

        List<RcsAntiPhishConfGlobal> globalConfList = rcsAntiPhishingGlobalManager.getGlobalConf();

        buildVo(confVo, globalConfList);

        return confVo;
    }

    /*
     * 更新单次修改
     * 
     * @see com.gopay.common.rcs.service.antiphish.manager.
     * RcsAntiPhishingManagerGlobalService
     * #updateGlobalConfBySql(com.gopay.common.rcs.vo.RcsAntiPhishConfGlobalVo)
     */
    @Override
    public void updateGlobalConfBySql(RcsAntiPhishConfGlobalVo newGlobalVo) {

        List<RcsAntiPhishConfGlobal> list = new ArrayList<RcsAntiPhishConfGlobal>();

        List<RcsAntiPhishConfEditLog> logList = new ArrayList<RcsAntiPhishConfEditLog>();

        buildUpdateList(newGlobalVo, list, logList);

        rcsAntiPhishingGlobalManager.updateGlobalConf(list,logList);
    }

    /**
     * 构建需要更新的规则对象 list
     * 
     * @param newGlobalVo
     * @param list
     * @param oldGlobalVo
     */
    private void buildUpdateList(RcsAntiPhishConfGlobalVo newGlobalVo, List<RcsAntiPhishConfGlobal> list,
            List<RcsAntiPhishConfEditLog> logList) {

        List<RcsAntiPhishConfGlobal> globalConfList = rcsAntiPhishingGlobalManager.getGlobalConf();
        if(null == globalConfList || globalConfList.isEmpty()){
        	RcsAntiPhishConfGlobal g1 = new RcsAntiPhishConfGlobal();
        	g1.setAntiPhishType("01");
        	g1.setIsEnabled(Integer.parseInt(newGlobalVo.getCheckDomain()));
        	g1.setLegalValue("");
        	g1.setGatewayChannel(GatewayChannel.GATEWAY_CHANNEL_00.value);
        	g1.setIllegalReqDicision("01");// 拒绝支付
        	list.add(g1);
        	
        	RcsAntiPhishConfGlobal g2 = new RcsAntiPhishConfGlobal();
        	g2.setAntiPhishType("03");
        	g2.setIsEnabled(Integer.parseInt(newGlobalVo.getCheckTimeStamp()));
        	g2.setLegalValue(newGlobalVo.getLegalValue());
        	g2.setGatewayChannel(GatewayChannel.GATEWAY_CHANNEL_00.value);
        	g2.setIllegalReqDicision("01");// 拒绝支付
        	list.add(g2);
        	
        	RcsAntiPhishConfGlobal g3 = new RcsAntiPhishConfGlobal();
        	g3.setAntiPhishType("04");// IP校验
        	g3.setIsEnabled("00".equals(newGlobalVo.getCheckOrderIP()) ? 0 : 1);// 00的时候关闭状态
        	g3.setLegalValue("");
        	g3.setGatewayChannel(GatewayChannel.GATEWAY_CHANNEL_00.value);
        	g3.setIllegalReqDicision(newGlobalVo.getCheckOrderIP());// 页面配置
        	list.add(g3);
        	
        	RcsAntiPhishConfGlobal g5 = new RcsAntiPhishConfGlobal();
        	g5.setAntiPhishType("05");// 验证码输入
        	g5.setIsEnabled("03".equals(newGlobalVo.getEnterValidityCode()) ? 0 : 1);// 03的时候关闭状态
        	g5.setLegalValue("");
        	g5.setGatewayChannel(newGlobalVo.getEnterValidityCode());// 页面配置
        	g5.setIllegalReqDicision("00");// 不存在此情况
        	list.add(g5);
        	// TODO
        	// 修改日志
        	return;
        }

        for (RcsAntiPhishConfGlobal confGlobal : globalConfList) {
            // 校验类型
            String antiPhishType = confGlobal.getAntiPhishType();

            if (antiPhishType.equals("01")) {

                if (!newGlobalVo.getCheckDomain().equals(confGlobal.getIsEnabled()+"")) {
                    // 记录日志
                    RcsAntiPhishConfEditLog log = getBaseLog(confGlobal.getConfSq(),newGlobalVo);
                    log.setValBeforeEdit(confGlobal.getIsEnabled() == 1 ? "开启" : "关闭");
                    log.setValAfterEdit(newGlobalVo.getCheckDomain().equals("1") ? "开启" : "关闭");
                    log.setEditNature(AntiPhishEditNatureEnum.ANTI_PHISH_TYPE.value);
                    logList.add(log);
                    // 精确域名是否修改
                    confGlobal.setIsEnabled(Integer.parseInt(newGlobalVo.getCheckDomain()));
                    list.add(confGlobal);
                }
            } else if (antiPhishType.equals("03")) {
                boolean isUpdate = false;
                // 开关是否变化
                if (!newGlobalVo.getCheckTimeStamp().equals(confGlobal.getIsEnabled()+"")) {
                    // 记录日志
                    RcsAntiPhishConfEditLog log =getBaseLog(confGlobal.getConfSq(), newGlobalVo);
                    log.setValBeforeEdit(confGlobal.getIsEnabled() == 1 ? "开启" : "关闭");
                    log.setValAfterEdit(newGlobalVo.getCheckTimeStamp().equals("1") ? "开启" : "关闭");
                    log.setEditNature(AntiPhishEditNatureEnum.DISJUNCTOR.value);
                    logList.add(log);

                    confGlobal.setIsEnabled(Integer.parseInt(newGlobalVo.getCheckTimeStamp()));
                    isUpdate = true;
                }
                if (!newGlobalVo.getLegalValue().equals(confGlobal.getLegalValue())) {
                    // 记录日志
                    RcsAntiPhishConfEditLog log = getBaseLog(confGlobal.getConfSq(), newGlobalVo);
                    log.setValBeforeEdit(confGlobal.getLegalValue());
                    log.setValAfterEdit(newGlobalVo.getLegalValue());
                    log.setEditNature(AntiPhishEditNatureEnum.LEGAL_VALUE.value);
                    logList.add(log);

                    confGlobal.setLegalValue(newGlobalVo.getLegalValue());
                    isUpdate = true;
                }
                if (isUpdate) {
                    list.add(confGlobal);
                }
            } else if (antiPhishType.equals("04")) {
                // 订单IP比对校验
                // 开关是否变化
                int updateIsEnabled = checkIPSwitchUpdae(confGlobal.getIsEnabled() + "", newGlobalVo.getCheckOrderIP());
                boolean isUpdate = false;
                if (updateIsEnabled <= 1) {
                    // 记录日志
                    confGlobal.setIsEnabled(updateIsEnabled);
                    isUpdate = true;
                }
                if(confGlobal.getIsEnabled()==0&&!newGlobalVo.getCheckOrderIP().equals(AntiPhishCheckOrderIpEnum.CLAOSE.value)){
                    // 记录日志
                    RcsAntiPhishConfEditLog log = getBaseLog(confGlobal.getConfSq(), newGlobalVo);
                    log.setValBeforeEdit(getCheckOrderIPName(confGlobal.getIllegalReqDicision()));
                    log.setValAfterEdit(getCheckOrderIPName(newGlobalVo.getCheckOrderIP()));
                    log.setEditNature(AntiPhishEditNatureEnum.ILLEGAL_REQ_DICISION.value);
                    logList.add(log);
                    confGlobal.setIllegalReqDicision(newGlobalVo.getCheckOrderIP());
                    isUpdate = true;
                }else if(!confGlobal.getIllegalReqDicision().equals(newGlobalVo.getCheckOrderIP())){
                    // 记录日志
                    RcsAntiPhishConfEditLog log = getBaseLog(confGlobal.getConfSq(), newGlobalVo);
                    log.setValBeforeEdit(getCheckOrderIPName(confGlobal.getIllegalReqDicision()));
                    log.setValAfterEdit(getCheckOrderIPName(newGlobalVo.getCheckOrderIP()));
                    log.setEditNature(AntiPhishEditNatureEnum.ILLEGAL_REQ_DICISION.value);
                    logList.add(log);
                    confGlobal.setIllegalReqDicision(newGlobalVo.getCheckOrderIP());
                    isUpdate = true;
                }
                if (isUpdate) {
                    list.add(confGlobal);
                }
            } else if (antiPhishType.equals("05")) {
                // 验证码输入校验否修改
               
                String nowEnterValidityCode = newGlobalVo.getEnterValidityCode();
                // 开关是否变化
                int updateIsEnabled = checkEnterValidityCode(confGlobal.getIsEnabled() + "", nowEnterValidityCode);

                boolean isUpdate = false;
                if (updateIsEnabled <= 1) {
                    confGlobal.setIsEnabled(updateIsEnabled);
                    isUpdate = true;
                }
                if(confGlobal.getIsEnabled()==0 && !nowEnterValidityCode.equals(AntiPhishGetwayChannelEnum.ALL_CLOSE.value)){
                    // 记录日志
                    RcsAntiPhishConfEditLog log = getBaseLog(confGlobal.getConfSq(), newGlobalVo);
                    log.setValBeforeEdit(getEnterValidatinName(confGlobal.getGatewayChannel()));
                    log.setValAfterEdit(getEnterValidatinName(newGlobalVo.getEnterValidityCode()));
                    log.setEditNature(AntiPhishEditNatureEnum.GATEWAY_CHANNEL.value);
                    logList.add(log);
                    
                    confGlobal.setGatewayChannel(nowEnterValidityCode);
                    isUpdate = true;
                }else if(!confGlobal.getGatewayChannel().equals(nowEnterValidityCode)){
                    // 记录日志
                    RcsAntiPhishConfEditLog log = getBaseLog(confGlobal.getConfSq(), newGlobalVo);
                    log.setValBeforeEdit(getEnterValidatinName(confGlobal.getGatewayChannel()));
                    log.setValAfterEdit(getEnterValidatinName(newGlobalVo.getEnterValidityCode()));
                    log.setEditNature(AntiPhishEditNatureEnum.GATEWAY_CHANNEL.value);
                    logList.add(log);
                    
                    confGlobal.setGatewayChannel(nowEnterValidityCode);
                    isUpdate = true;
                }
                if (isUpdate) {
                    list.add(confGlobal);
                }
            }
        }
    }
    private String getCheckOrderIPName(String checkOrderIP){
        if(checkOrderIP.equals(AntiPhishCheckOrderIpEnum.CLAOSE.value)){
            return "关闭";
        }else if(checkOrderIP.equals(AntiPhishCheckOrderIpEnum.REJECT.value)){
            return "拒绝支付";
        }else if(checkOrderIP.equals(AntiPhishCheckOrderIpEnum.WARN.value)){
            return "提示用户";
        }
        return null;
    }
    private String getEnterValidatinName(String enterValidatiCode){
        if(enterValidatiCode.equals(AntiPhishGetwayChannelEnum.ALL_CLOSE.value)){
            return "关闭";
        }
        if(enterValidatiCode.equals(AntiPhishGetwayChannelEnum.DIRECT_BANK.value)){
            return "直连开启";
        }
        if(enterValidatiCode.equals(AntiPhishGetwayChannelEnum.UN_DIRECT_BANK.value)){
            return "非直连开启";
        }
        if(enterValidatiCode.equals(AntiPhishGetwayChannelEnum.ALL_OPEN.value)){
            return "全部开启";
        }
        return null;
    }
    
    /**
     * 一个基础的log对象，初始化共有对象
     * @param confSq
     * @param operId
     * @param operName
     * @return
     */
    private RcsAntiPhishConfEditLog getBaseLog(int confSq,RcsAntiPhishConfGlobalVo newGlobalVo){
        RcsAntiPhishConfEditLog log = new RcsAntiPhishConfEditLog();
        log.setOperConfType("01");
        log.setOperId(newGlobalVo.getOperId());
        log.setOperName(newGlobalVo.getOperName());
        log.setOperTargetSq(confSq+ "");
        return log;
    }

    /**
     * 构建返回VO对象
     * 
     * @param confVo
     * @param strGlobalConf
     */
    private void buildVo(RcsAntiPhishConfGlobalVo confVo, List<RcsAntiPhishConfGlobal> globalConfList) {
        for (RcsAntiPhishConfGlobal confGlobal : globalConfList) {
            // 校验类型
            String antiPhishType = confGlobal.getAntiPhishType();
            // 开关
            String isEnabled = confGlobal.getIsEnabled() + "";

            if (antiPhishType.equals("01")) {
                // 精确域名校验
                confVo.setCheckDomain(isEnabled);
            } else if (antiPhishType.equals("03")) {
                // 时间戳间隔校验
                confVo.setCheckTimeStamp(isEnabled);
                confVo.setLegalValue(confGlobal.getLegalValue());
            } else if (antiPhishType.equals("04")) {
                // 订单IP校验
                setCheckOrderIP(confVo, confGlobal, isEnabled);
            } else if (antiPhishType.equals("05")) {
                // 验证码输入
                setEnterValidityCode(confVo, confGlobal, isEnabled);
            }
        }
    }

    private void setEnterValidityCode(RcsAntiPhishConfGlobalVo confVo, RcsAntiPhishConfGlobal confGlobal,
            String isEnabled) {
        String enterValidityCode = confGlobal.getGatewayChannel();
        if (isEnabled.equals("0")) {
            enterValidityCode = AntiPhishGetwayChannelEnum.ALL_CLOSE.value;
        }
        confVo.setEnterValidityCode(enterValidityCode);
    }

    private void setCheckOrderIP(RcsAntiPhishConfGlobalVo confVo, RcsAntiPhishConfGlobal confGlobal, String isEnabled) {
        String checkOrderIPVal = null;
        if (isEnabled.equals("1")) {
            checkOrderIPVal = confGlobal.getIllegalReqDicision();
        } else {
            checkOrderIPVal = AntiPhishCheckOrderIpEnum.CLAOSE.value;
        }
        confVo.setCheckOrderIP(checkOrderIPVal);
    }

    /**
     * 校验IP开关是否更新
     * 
     * @param oldGlobalVo
     * @param newGlobalVo
     * @return
     */
    private int checkIPSwitchUpdae(String oldStatus, String nowStatus) {
        String close = AntiPhishCheckOrderIpEnum.CLAOSE.value;
        if (oldStatus.equals("0") && !nowStatus.equals(close)) {
            return 1;
        }
        if (!oldStatus.equals("0") && nowStatus.equals(close)) {
            return 0;
        }
        return 2;
    }

    /**
     * 验证码输入开关是否更新
     * 
     * @param oldGlobalVo
     * @param newGlobalVo
     * @return
     */
    private int checkEnterValidityCode(String oldStatus, String nowStatus) {
        String close = AntiPhishGetwayChannelEnum.ALL_CLOSE.value;
        if (oldStatus.equals("0") && !nowStatus.equals(close)) {
            return 1;
        }
        if (!oldStatus.equals("0") && nowStatus.equals(close)) {
            return 0;
        }
        return 2;
    }
}
